layui.use(['form', 'jquery', 'layer', 'table', 'laydate', 'upload'], function() {
	var form = layui.form;
	var $ = layui.$;
	var table = layui.table;
	var layer = layui.layer;
	var laydate = layui.laydate;
	var upload = layui.upload;

	var lay_filter = 'tpl_recruit' + recruit_id;
	form.val(lay_filter, recruit_form_value);
	// 上传
	upload.render({
		elem: '#icon_pictures_upload' + recruit_id,
		url: upload_uri,
		accept: 'images',
		acceptMime: 'image/*',
		auto: true,
		field: 'icon_pictures_upload' + recruit_id,
		multiple: true,
		drag: true,
		before: function(obj) {
			layer.load();
		},
		done: function(res, index, upload) {
			if(res.code == 0) {
				var item = this.item;
				if(item == undefined){
					item = $('.layui-upload-drag');
				}
				var html = createUploadHtml('icon_pictures[]', res.data);
				item.prev('.layui-upload-list').append(html);
				layer.msg('上传成功', {
					icon: 1
				});
			} else {
				layer.msg(e.msg, {
					icon: 2
				});
			}

			layer.closeAll('loading');
		},
		error: function(index, upload) {
			layer.msg('网络错误', {
				icon: 2
			});
			layer.closeAll('loading');
		}
	});
	// 初始化图片
	console.info(recruit_form_value);
	if(recruit_form_value.icon_pictures != undefined && recruit_form_value.icon_pictures.length > 0) {
		for(var i in recruit_form_value.icon_pictures) {
			var html = createUploadHtml('icon_pictures[]', recruit_form_value.icon_pictures[i]);
			$('#layui-upload-list' + recruit_id).append(html);
		}
	}
	// 监听提交
	form.on('submit(submit_recruit)', function(data) {
		var field = data.field;
		var url = $('form[lay-filter="' + lay_filter + '"]').attr('action');
		if(field.recruit_name == '') {
			layer.msg('文章标题必须填写', {
				icon: 2,
				amim: 6
			})
			$('input[name="recruit_name"]').addClass('.layui-form-danger').focus();
			return false;
		}
		$.post(url, field, function(data) {
			if(data.code == 0) {
				layer.msg('保存成功', {
					icon: 1
				});
				// 关闭当前页 刷新父页
				$('.layui-body .layui-tab[lay-filter="content"] li.layui-this .layui-tab-close').trigger('click');
				$('.layui-body .layui-tab[lay-filter="content"] li[lay-id="recruit"]').trigger('click');
				// 父页面点击
				$('.layui-body .layui-tab[lay-filter="content"] li[lay-id="recruit_list"]').trigger('click');
				// 表格重载
				table.reload('recruit_list');
			} else {
				layer.msg(data.msg, {
					icon: 2,
					anim: 6
				});
			}
		}, 'json')
		return false;
	})

})